package com.wac.data.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wac.data.entity.Article;
import com.wac.data.entity.Dynamic;
import com.wac.data.entity.Message;
import com.wac.data.entity.State;
import com.wac.data.mapper.*;
import com.wac.data.service.StateService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wac.data.service.UserService;
import com.wac.data.utils.DataListUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author beta
 * @since 2023-03-16
 */
@Service
public class StateServiceImpl extends ServiceImpl<StateMapper, State> implements StateService {

	@Resource
	private StateMapper stateMapper;
	@Resource
	private ArticleMapper articleMapper;
	@Resource
	private DynamicMapper dynamicMapper;
	@Resource
	private UserMapper userMapper;
	@Resource
	private MessageMapper messageMapper;

	private Page<State> thePage;

	@Override
	public boolean isFollow(String id, String userId) {
		if (stateMapper.selectOne(new QueryWrapper<State>().eq("follow_id", id).eq("user_id", userId)) != null) {
			return true;
		} else {
			return false;
		}
	}

	@Override
	public boolean isFriend(String id, String userId) {
		List<String> friendsId = new ArrayList<>();
		userMapper.getFriend(userId).forEach(friend -> {
			if (friend.getFriend().equals(id)) {
				friendsId.add(friend.getFriend());
			}
		});
		if (!friendsId.isEmpty()) {
			return true;
		}
		return false;
	}

	@Override
	public boolean toFollow(String id, String userId, String type) {
		if (userId == null) {
			return false;
		}
		if (isFollow(id, userId)) {
			if (type.equals("user")) {
				if (stateMapper.delete(new QueryWrapper<State>().eq("follow_id", id).eq("user_id", userId)) == 1 && messageMapper.delete(new QueryWrapper<Message>().eq("send_id", userId).eq("receive_id", id)) == 1) {
					if (isFriend(id, userId)) {
						if (userMapper.deleteFriend(id, userId) == 1 && userMapper.deleteFriend(userId, id) == 1) {
							return true;
						}
					}
					return true;
				}
			} else {
				if (stateMapper.delete(new QueryWrapper<State>().eq("follow_id", id).eq("user_id", userId)) == 1) {
					return true;
				}
			}

			return false;
		}
		State state = new State();
		state.setFollowId(id);
		state.setUserId(userId);
		state.setType(type);
		if (stateMapper.insert(state) == 1) {
			return true;
		}
		return false;
	}

	@Override
	public boolean addView(String id, int type) {
		if (type == 1) {
			Article article = new Article();
			article.setId(id);
			article.setView(articleMapper.selectById(id).getView() + 1);
			if (articleMapper.updateById(article) == 1) {
				return true;
			}
		} else {
			Dynamic dynamic = new Dynamic();
			dynamic.setId(id);
			dynamic.setView(dynamicMapper.selectById(id).getView() + 1);
			if (dynamicMapper.updateById(dynamic) == 1) {
				return true;
			}
		}

		return false;
	}

	@Override
	public Map<String, Object> getUserBangumiList(int page, String id) {
		thePage = new Page<>(page, 5);
		stateMapper.selectPage(thePage, new QueryWrapper<State>().select("follow_id", "time").eq("type", "bangumi").eq("user_id", id).orderByDesc("time"));
		return new DataListUtil().data(thePage);
	}


}
